package com.ws.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;

import com.ws.config.EasyWebConfig;

public class UploadFileUtil {
	private static final String UPLOAD_DIR = "D:\\uploadfile\\";
	static {
		File file = new File(EasyWebConfig.STATIC_DIR);
		if(!file.exists()) {
			file.mkdirs();
		}
	}
	public static String uploadFile(MultipartFile file) {
		//生成一个新的文件名
		String fileName = file.getOriginalFilename();//获取上传文件的原文件名字
		String lastName = fileName.substring(fileName.lastIndexOf("."));//获取文件后缀名
		String newName = UUID.randomUUID() + lastName;//给文件生成一个新名字
		//将file文件保存到UPLOAD_DIR路径中
		try {
			file.transferTo(new File(EasyWebConfig.STATIC_DIR + newName));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//返回文件路径以及文件名
		return newName;
	}
}
